﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client;
using PPI.Client.Logic;
using PPI.Client.Presenter;
using PPI.Client.Utils;
using PPI.Entities;
using PPI.Entities.Lookup;
using PPI.Entities.XML;
using PPI.General;
using PPIControls.Pole;
using VMind.Global;

namespace PPI.CRUD
{
    public partial class frmVisitCrud : frmBase, IVisitCrud
    {
        private List<EHandheld> m_Handhelds;
        private List<EVendor_Project> m_Projects;
        private List<EVendor_Contact> m_Contacts;
        private List<EVendor> m_Vendors;
        private List<EVendor_Visit_Type> m_VisitTypes;
        private List<EXMissionStructures_TopType> m_VisitTopTypes;
        private List<EXMissionStructures_PoleClass> m_Classes;
        private List<EXMissionStructures_PoleSpecies> m_Species;
        private List<EXMissionStructures_PoleTreatment> m_Treatments;
        private EVendor_Visit_XMissionStructure m_Visit;
        private EXmlOutboxBase m_OutboxBase;
        private VisitCrudPresenter m_Presenter;
        private EVendor_Visit_XMissionStructures_Pole m_SelectedPole;
        private List<EVendor_Visit_XMissionStructures_Image> m_deletedImages;
        private List<EVendor_Visit_XMissionStructures_Pole> m_deletedPoles;
        private bool m_ShowImages;


        #region Properties

        public EVendor_Visit_XMissionStructure Visit
        {
            get { return m_Visit; }
            set { m_Visit = value; }
        }

        public List<EVendor_Visit_XMissionStructures_Image> DeleteImages
        {
            get { return m_deletedImages; }
        }

        public List<EVendor_Visit_XMissionStructures_Pole> DeletePoles
        {
            get { return m_deletedPoles; }
        }

        public string ConnectionString
        {
            get { return m_OutboxBase.ConnectionString; }
        }

        public EXmlOutboxBase Database
        {
            get { return m_OutboxBase; }
        }

        public EXmlOutboxBase OutboxBase
        {
            get { return m_OutboxBase; }
            set { m_OutboxBase = value; }
        }


        public bool ShowImages
        {
            get { return m_ShowImages; }
            set { m_ShowImages = value; }
        }

        #endregion


        public frmVisitCrud()
        {
            InitializeComponent();

            base.MenuMain.Visible = false;

            this.FormSize = FormSizes.Large;

            m_Presenter = new VisitCrudPresenter(this);
            
            try
            {
                LoadCombos();
            }
            catch(Exception ex)
            {
                RegisterError.SaveError(ex);
                MessageBox.Show(ex.Message);
            }
            
        }


        #region IVisitCrud
        public int ProgressBarValue
        {
            set { throw new NotImplementedException(); }
        }

        public string ErrorMessage
        {
            set { throw new NotImplementedException(); }
        }
        #endregion



        #region Control Events

        private void frmVisitCrud_Load(object sender, EventArgs e)
        {
            m_deletedImages = new List<EVendor_Visit_XMissionStructures_Image>();
            m_deletedPoles = new List<EVendor_Visit_XMissionStructures_Pole>();
            try
            {
                poleTree1.DisableDragAndDrop(m_ShowImages);
                tabImages.Visible = m_ShowImages;
                CleanPoleValues();
                cleanVisit();
                if (m_Visit != null)
                {
                    LoadVisitComboValues();
                    LoadVisitValues();
                    LoadVisitImages();
                    LoadPoleTree();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void frmVisitCrud_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                ActivateDesactivatePole(false);
                CleanPoleValues();
                cleanVisit();
                CleanImageResource();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }



        private void cleanVisit()
        {
            ResetVisitComboIndexes();
            dateDate.Value = DateTime.Now;
           
        }

        private void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ddlProject.Enabled = (ddlVendor.SelectedIndex > 0);
                ddlContact.Enabled = (ddlVendor.SelectedIndex > 0);

                int _selectedValue = 0;

                //Load de ProjectCombo
                if (ddlVendor.SelectedIndex > 0)
                {
                    ListBoxItem _selectedItem = (ListBoxItem)ddlVendor.SelectedItem;

                    _selectedValue = ConvertHelper.ToInt32(_selectedItem.Value);

                    m_Projects = m_Vendors.Find(delegate(EVendor v) { return v.Id == _selectedValue; }).Projects;
                    DropdownHelper.FillDropDown(ddlProject, m_Projects, typeof(EVendor_Project), "Name", "Vendor_ProjectId", "Select an option...");

                    m_Contacts = m_Vendors.Find(delegate(EVendor v) { return v.Id == _selectedValue; }).Contacts;
                    DropdownHelper.FillDropDown(ddlContact, m_Contacts, typeof(EVendor_Contact), "Name", "Vendor_ContactId", "Select an option...");


                    if (m_Visit.Vendor_Project.Id != Int32.MinValue)
                        ddlProject.SelectedIndex = DropdownHelper.GetItemIndex(ddlProject, m_Visit.Vendor_Project.Id.ToString());

                    if (m_Visit.Vendor_Contact.Id != Int32.MinValue)
                        ddlContact.SelectedIndex = DropdownHelper.GetItemIndex(ddlContact, m_Visit.Vendor_Contact.Id.ToString());

                    CheckVisitComboIndexes();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void ddlVisitTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ddlTopType.Enabled = (ddlVisitTypes.SelectedIndex > 0);

                int _selectedValue = 0;

                //Load de ProjectCombo
                if (ddlVisitTypes.SelectedIndex > 0)
                {
                    ListBoxItem _selectedItem = (ListBoxItem)ddlVisitTypes.SelectedItem;

                    _selectedValue = ConvertHelper.ToInt32(_selectedItem.Value);

                    m_VisitTopTypes = m_VisitTypes.Find(delegate(EVendor_Visit_Type v) { return v.Vendor_Visit_TypeId == _selectedValue; }).Tops;
                    DropdownHelper.FillDropDown(ddlTopType, m_VisitTopTypes, typeof(EXMissionStructures_TopType), "Name", "XMissionStructures_TopTypeId", "Select an option...");
                    DropdownHelper.FillDropDown(ddlTopTypePole, m_VisitTopTypes, typeof(EXMissionStructures_TopType), "Name", "XMissionStructures_TopTypeId", "Select an option...");

                    if (m_Visit.TopType.XMissionStructures_TopTypeId != Int32.MinValue)
                    {
                        ddlTopType.SelectedIndex = DropdownHelper.GetItemIndex(ddlTopType, m_Visit.TopType.XMissionStructures_TopTypeId.ToString());
                        ddlTopTypePole.SelectedIndex = DropdownHelper.GetItemIndex(ddlTopTypePole, m_Visit.TopType.XMissionStructures_TopTypeId.ToString());
                    }



                    CheckVisitComboIndexes();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }

        }

        private void btnSaveVisit_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateVisit(true))
                {
                    this.Cursor = Cursors.WaitCursor;
                    SaveVisitValues();
                    m_Presenter.SaveVisit();
                    this.Cursor = Cursors.Default;
                    this.DialogResult = DialogResult.OK;
                    this.Close();

                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void ddlTopType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //update all Poles

            try
            {
                if (ddlTopType.SelectedIndex > 0)
                {
                    ListBoxItem selectedItem = (ListBoxItem)ddlTopType.SelectedItem;
                    ddlTopTypePole.SelectedIndex = DropdownHelper.GetItemIndex(ddlTopTypePole, selectedItem.Value);
                    if (ConvertHelper.ToInt32(selectedItem.Value) > 0)
                    {
                        foreach (EVendor_Visit_XMissionStructures_Pole p in m_Visit.Poles)
                        {
                            p.TopType = new EXMissionStructures_PoleTopType(ConvertHelper.ToInt32(selectedItem.Value), selectedItem.Text);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateVisit(true))
                {
                    MessageBox.Show("There is at least one field with error. Please solve this issue and then retry to save the current pole");
                }
                if (ValidatePole(true))
                {
                    AddUpdatePole();
                    ActivateDesactivatePole(false);
                    CleanPoleValues();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
            
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            ActivateDesactivatePole(false);
            CleanPoleValues();
        }

        private void btnSearchImage_Click(object sender, EventArgs e)
        {
            SearchImage();
        }

        private void ddlListImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlListImages.SelectedIndex > -1)
                {
                    EBaseAssetImage tmpImage = (EBaseAssetImage)ddlListImages.SelectedItem;
                    LoadImageData(tmpImage, pbxSelectedImage);

                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void ddlListImages_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (ddlListImages.SelectedIndex > -1)
                {
                    try
                    {
                        EVendor_Visit_XMissionStructures_Image tmpImage = (EVendor_Visit_XMissionStructures_Image)ddlListImages.SelectedItem;
                        DeleteVisitImage(tmpImage);

                    }
                    catch (Exception ex)
                    {
                        RegisterError.SaveError(ex);
                    }
                }
            }
        }


        #region PoleTreeEVents

        private void poleTree1_ClickedEditImage(object sender, PPIControls.Pole.PoleTreeEventArgs e)
        {
            try
            {
                EBaseImage selectedImage = null;
                if (e.PoleId != Int32.MinValue)
                    selectedImage = FindPole(e.PoleId).Images.Find(delegate(EBaseAssetImage i) { return i.Id == e.ImageId; });
                else
                    selectedImage = m_Visit.Images.Find(delegate(EBaseAssetImage i) { return i.Id == e.ImageId; });
                LoadImageData(selectedImage, pbxPoleImagePreview);
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }

        }

        private void poleTree1_ClickedEditPole(object sender, PPIControls.Pole.PoleTreeEventArgs e)
        {
            try
            {
                ActivateDesactivatePole(true);
                CleanPoleValues();
                EBasePole selectedPole = FindPole(e.PoleId);
                m_SelectedPole = (EVendor_Visit_XMissionStructures_Pole)selectedPole;
                LoadPoleData(selectedPole);
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }

        }


        private void poleTree1_ClickedNewPole(object sender, PPIControls.Pole.PoleTreeEventArgs e)
        {
            try
            {
                CleanPoleValues();
                ActivateDesactivatePole(true);

            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void poleTree1_ClickedDeletePole(object sender, PoleTreeEventArgs e)
        {
            try
            {
                EBasePole poleToDelete = FindPole(e.PoleId);
                if (poleToDelete != null)
                {
                    m_Visit.Poles.Remove(poleToDelete);
                    m_deletedPoles.Add((EVendor_Visit_XMissionStructures_Pole)poleToDelete);
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
           
        }

        #endregion
        
        #endregion

        private void LoadCombos()
        {
            m_Handhelds = LCacheDataSingleton.Instance.Handhelds;
            DropdownHelper.FillDropDown(ddlHandheld, m_Handhelds, typeof(EHandheld), "Name", "Id", "Select an option...");


            m_Classes = LCacheDataSingleton.Instance.Class;
            DropdownHelper.FillDropDown(ddlClass, m_Classes, typeof(EXMissionStructures_PoleClass), "Name", "Id", "Select an option...");

            m_Species = LCacheDataSingleton.Instance.Specie;
            DropdownHelper.FillDropDown(ddlSpecies, m_Species, typeof(EXMissionStructures_PoleSpecies), "Name", "Id", "Select an option...");

            m_Treatments = LCacheDataSingleton.Instance.Treatment;
            DropdownHelper.FillDropDown(ddlTreatment, m_Treatments, typeof(EXMissionStructures_PoleTreatment), "Name", "Id", "Select an option...");

            DataTable dtLength = FillSpecificDataTableHelper.FillDataTable(55, 80, 5);
            DropdownHelper.FillDropDown(ddlLength, dtLength, "Name", "Id", "Select an option...");

            DataTable dtYears = FillSpecificDataTableHelper.FillDataTable(1944, DateTime.Now.Year, 1);
            DropdownHelper.FillDropDown(ddlYear, dtYears, "Name", "Id", "Select an option...");
            DropdownHelper.FillDropDown(ddlLVisit, dtYears, "Name", "Id", "Select an option...");


            m_Vendors = LCacheDataSingleton.Instance.Vendor;
            DropdownHelper.FillDropDown(ddlVendor, m_Vendors, typeof(EVendor), "Name", "VendorId", "Select an option...");
            m_VisitTypes = LCacheDataSingleton.Instance.VisitTypes;
            DropdownHelper.FillDropDown(ddlVisitTypes, m_VisitTypes, typeof(EVendor_Visit_Type), "Name", "Vendor_Visit_TypeId", "Select an option...");



        }

        private void LoadVisitValues()
        {
            positionVisit.SetPosition(m_Visit);
            dateDate.Value = m_Visit.Date;
            

        }

        private void LoadPoleTree()
        {
            try
            {
                poleTree1.CleanTree();
                poleTree1.FillNode(m_Visit);
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void LoadVisitComboValues()
        {
            if (m_Visit.Vendor_Project.Vendor.Id != Int32.MinValue)
                ddlVendor.SelectedIndex = DropdownHelper.GetItemIndex(ddlVendor, m_Visit.Vendor_Project.Vendor.Id.ToString());

            if(m_Visit.Vendor_Visit_Type.Vendor_Visit_TypeId != Int32.MinValue)
                ddlVisitTypes.SelectedIndex = DropdownHelper.GetItemIndex(ddlVisitTypes, m_Visit.Vendor_Visit_Type.Vendor_Visit_TypeId.ToString());

            if (m_Visit.Handheld.Id != Int32.MinValue)
                ddlHandheld.SelectedIndex = DropdownHelper.GetItemIndex(ddlHandheld, m_Visit.Handheld.Id.ToString());


            CheckVisitComboIndexes();
        }

        private void LoadImageData(EBaseImage image, PictureBox pbx)
        {
            if (image.ImageFile.Exists)
            {
                Bitmap _Bitmap = new Bitmap(image.ImageFile.FullName);
                pbx.Image = _Bitmap;
            }
        }

        private void LoadImageData(string fullpath, PictureBox pbx)
        {
            if (File.Exists(fullpath))
            {
                Bitmap _Bitmap = new Bitmap(fullpath);
                pbx.Image = _Bitmap;
            }
        }

        private EBasePole FindPole(int poleId)
        {
            return m_Visit.Poles.Find(delegate(EBasePole p) { return p.Id == poleId; });
        }

        private void LoadPoleData(EBasePole pole)
        {
            
            if(pole.Year != Int32.MinValue)
            {
                ddlYear.SelectedIndex = DropdownHelper.GetItemIndex(ddlYear, pole.Year.ToString());
            }
            if(pole.Treatment.Id != Int32.MinValue)
            {
                ddlTreatment.SelectedIndex = DropdownHelper.GetItemIndex(ddlTreatment, pole.Treatment.Id.ToString());
            }
            if(pole.TopType.Id != Int32.MinValue)
            {
                ddlTopTypePole.SelectedIndex = DropdownHelper.GetItemIndex(ddlTopTypePole, pole.TopType.Id.ToString());
            }
            if(pole.Species.Id != Int32.MinValue)
            {
                ddlSpecies.SelectedIndex = DropdownHelper.GetItemIndex(ddlSpecies, pole.Species.Id.ToString());
            }
            if(pole.LVisit != Int32.MinValue)
            {
                ddlLVisit.SelectedIndex = DropdownHelper.GetItemIndex(ddlLVisit, pole.LVisit.ToString());
            }


            if(pole.Length != double.MinValue)
            {
                ddlLength.SelectedIndex = DropdownHelper.GetItemIndex(ddlLength, pole.Length.ToString());
            }

            if(pole.Class.Id != Int32.MinValue)
            {
                ddlClass.SelectedIndex = DropdownHelper.GetItemIndex(ddlClass, pole.Class.Id.ToString());
            }

            positionPole.SetPosition(pole);
            txtLVisitor.Text = pole.LVisitor;
            txtNumber.Text = pole.Number;

            CheckPoleComboIndexes();
            
        }

        private void CheckPoleComboIndexes()
        {
            CheckComboIndex(ddlYear);
            CheckComboIndex(ddlTreatment);
            CheckComboIndex(ddlSpecies);
            CheckComboIndex(ddlLength);
            CheckComboIndex(ddlClass);
            CheckComboIndex(ddlLVisit);
        }

        private void ResetPoleComboIndexes()
        {
            ResetComboIndex(ddlYear);
            ResetComboIndex(ddlTreatment);
            ResetComboIndex(ddlSpecies);
            ResetComboIndex(ddlLength);
            ResetComboIndex(ddlClass);
            ResetComboIndex(ddlLVisit);

        }

        private void CheckVisitComboIndexes()
        {
            CheckComboIndex(ddlVendor);
            CheckComboIndex(ddlVisitTypes);
            CheckComboIndex(ddlProject);
            CheckComboIndex(ddlTopType);
            CheckComboIndex(ddlContact);
            CheckComboIndex(ddlHandheld);

        }

        private void ResetVisitComboIndexes()
        {
            ResetComboIndex(ddlVendor);
            ResetComboIndex(ddlVisitTypes);
            ResetComboIndex(ddlProject);
            ResetComboIndex(ddlTopType);
            ResetComboIndex(ddlContact);
            ResetComboIndex(ddlHandheld);

        }

        private static void CheckComboIndex(ComboBox combo)
        {
            if (combo.Items.Count > 0)
            {
                if (combo.SelectedIndex == -1)
                    combo.SelectedIndex = 0;
            }
        }

        private static void ResetComboIndex(ComboBox combo)
        {
            if (combo.Items.Count > 0)
            {
                combo.SelectedIndex = 0;
            }
        }

        private void CleanPoleValues()
        {
            m_SelectedPole = new EVendor_Visit_XMissionStructures_Pole();
            m_SelectedPole.Asset = m_Visit;

            pbxPoleImagePreview.Image = null;
            ResetPoleComboIndexes();
            positionPole.CleanPositions();
            txtLVisitor.Text = "";
            txtNumber.Text = "";
        }

        private void AddUpdatePole()
        {
            if (m_SelectedPole == null)
                return;

            try
            {
                positionPole.GetPosition(m_SelectedPole);
                m_SelectedPole.Number = txtNumber.Text;
                m_SelectedPole.LVisitor = txtLVisitor.Text;


                ListBoxItem _selectedItem = null;
                _selectedItem = (ListBoxItem)ddlYear.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_SelectedPole.Year = ConvertHelper.ToInt32(_selectedItem.Value);

                _selectedItem = (ListBoxItem)ddlTreatment.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_SelectedPole.Treatment = new EXMissionStructures_PoleTreatment(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);

                _selectedItem = (ListBoxItem)ddlSpecies.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_SelectedPole.Species = new EXMissionStructures_PoleSpecies(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);

                _selectedItem = (ListBoxItem)ddlClass.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_SelectedPole.Class = new EXMissionStructures_PoleClass(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);

                _selectedItem = (ListBoxItem)ddlTopTypePole.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_SelectedPole.TopType = new EXMissionStructures_PoleTopType(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);


                _selectedItem = (ListBoxItem)ddlLength.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_SelectedPole.Length = ConvertHelper.ToInt32(_selectedItem.Value);

                _selectedItem = (ListBoxItem)ddlLVisit.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_SelectedPole.LVisit = ConvertHelper.ToInt32(_selectedItem.Value);


                if (m_SelectedPole.Id == Int32.MinValue)
                {
                    if (!m_Visit.Poles.Contains(m_SelectedPole))
                        m_Visit.Poles.Add(m_SelectedPole);
                    LoadPoleTree();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private bool ValidatePole(bool goToTab)
        {
            bool validResult = (ValidateAll() && positionPole.ValidatePosition());
            if (!validResult && goToTab)
                mainTabControl.SelectedTab = tabPoles;
            return validResult;

        }

        private bool ValidateVisit(bool goToTab)
        {
            bool validResult = (ValidateAll() && positionVisit.ValidatePosition());
            if (!validResult && goToTab)
                mainTabControl.SelectedTab = tabVisit;
            return validResult;

        }

        private void DeleteVisitImage(EVendor_Visit_XMissionStructures_Image image)
        {
            try
            {
                if (image.Pole.Id != Int32.MinValue)
                {
                    MessageBox.Show("Cannot delete this image, its related with pole " + image.Pole.Id);
                }
                else
                {
                    if (image.Id != Int32.MinValue)
                        m_deletedImages.Add(image);
                    m_Visit.Images.Remove(image);
                    LoadVisitImages();
                    LoadPoleTree();
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void DeletePoleImage(EBaseAssetImage image)
        {
            image.Pole = new EBasePole();
        }

        private void SearchImage()
        {
            try
            {
                OpenFileDialog _OpenFileDialog = new OpenFileDialog();
                DialogResult _DialogResult = _OpenFileDialog.ShowDialog();
                EVendor_Visit_XMissionStructures_Image _Image;
                FileInfo _SelectedFile;
                switch (_DialogResult)
                {
                    case DialogResult.OK:
                    case DialogResult.Yes:
                        _SelectedFile = new FileInfo(_OpenFileDialog.FileName);
                        _Image = new EVendor_Visit_XMissionStructures_Image();
                        _Image.ImageFile = _SelectedFile;
                        _Image.Asset = m_Visit;
                        m_Visit.Images.Add(_Image);
                        LoadPoleTree();
                        LoadVisitImages();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
            
        }

        private void LoadVisitImages()
        {
            ddlListImages.DataSource = null;
            ddlListImages.Items.Clear();
            ddlListImages.DisplayMember = "Title";
            ddlListImages.DataSource = m_Visit.Images;
        }

        private void SaveVisitValues()
        {
            try
            {
                positionVisit.GetPosition(m_Visit);
                ListBoxItem _selectedItem = null;
                m_Visit.Date = dateDate.Value;

                _selectedItem = (ListBoxItem)ddlVisitTypes.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_Visit.Vendor_Visit_Type = new EVendor_Visit_Type(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);

                _selectedItem = (ListBoxItem)ddlTopType.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_Visit.TopType = new EXMissionStructures_TopType(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);

                _selectedItem = (ListBoxItem)ddlHandheld.SelectedItem;
                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    m_Visit.Handheld = new EHandheld(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);


                _selectedItem = (ListBoxItem)ddlVendor.SelectedItem;

                if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                {
                    Int32 idVendor = ConvertHelper.ToInt32(_selectedItem.Value);
                    _selectedItem = (ListBoxItem)ddlProject.SelectedItem;
                    if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    {
                        m_Visit.Vendor_Project = new EVendor_Project(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);
                        m_Visit.Vendor_Project.Vendor = new EVendor(idVendor);
                    }

                    _selectedItem = (ListBoxItem)ddlContact.SelectedItem;
                    if (ConvertHelper.ToInt32(_selectedItem.Value) > 0)
                    {
                        m_Visit.Vendor_Contact = new EVendor_Contact(ConvertHelper.ToInt32(_selectedItem.Value), _selectedItem.Text);
                        m_Visit.Vendor_Contact.Vendor = new EVendor(idVendor);
                    }
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void ActivateDesactivatePole(bool condition)
        {
            btnSave.Enabled = condition;
            btnDiscard.Enabled = condition;
            poleTree1.Enabled = !condition;
        }

        private void CleanImageResource()
        {
            pbxPoleImagePreview.Image = null;
            pbxSelectedImage.Image = null;
        }



        




    }
}
